package com.jiuzhou.guanwang.jzcp.activity;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.view.MenuItem;
import android.widget.Toast;

import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.adapter.ViewPagerAdapter;
import com.jiuzhou.guanwang.jzcp.base.BaseActivity;
import com.jiuzhou.guanwang.jzcp.fragment.AwardFragment;
import com.jiuzhou.guanwang.jzcp.fragment.ExpertFragment;
import com.jiuzhou.guanwang.jzcp.fragment.HomeFragment;
import com.jiuzhou.guanwang.jzcp.fragment.NewsFragment;
import com.jiuzhou.guanwang.jzcp.fragment.SportFragment;
import com.jiuzhou.guanwang.jzcp.presenter.HomePresenter;
import com.jiuzhou.guanwang.jzcp.utils.BottomNavigationViewHelper;
import com.jiuzhou.guanwang.jzcp.view.HomeView;
import com.jiuzhou.guanwang.jzcp.widget.NoScrollViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeActivity extends BaseActivity<HomePresenter> implements HomeView {
    @BindView(R.id.viewPager)
    NoScrollViewPager viewPager;
    @BindView(R.id.navigation)
    BottomNavigationView navigation;
    private ArrayList<Integer> BannerList = new ArrayList<>();
    private ViewPagerAdapter adapter;

    @Override
    protected HomePresenter initPresenter() {
        return new HomePresenter(this);
    }


    @Override
    protected void initialize() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
//        Banner banner = (Banner) findViewById(R.id.banner);
//        BannerList.add(R.drawable.banner1);
//        BannerList.add(R.drawable.banner2);
//        BannerList.add(R.drawable.banner3);
//        banner.setImages(BannerList)
//                .setImageLoader(new GlideImageLoader())
//                .start();
        initView();

    }

    private void setupViewPager(NoScrollViewPager viewPager) {
        viewPager.setScroll(false);
        adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFragment(HomeFragment.newInstance("首页"));
        adapter.addFragment(AwardFragment.newInstance("开奖"));
        adapter.addFragment(SportFragment.newInstance("世界杯"));
        adapter.addFragment(NewsFragment.newInstance("比分"));
        adapter.addFragment(ExpertFragment.newInstance("专家"));
        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(4);
    }

    private void initView() {
        BottomNavigationViewHelper.disableShiftMode(navigation);
        setupViewPager(viewPager);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    long defTime = 0;

    @Override
    public void onBackPressed() {
        if (defTime == 0 || (System.currentTimeMillis() - defTime) > 2000) {
            Toast.makeText(this, "再按一次离开", Toast.LENGTH_SHORT).show();
            defTime = System.currentTimeMillis();
        } else {
            super.onBackPressed();
        }
    }

    public void setTabSelected(int index){
        navigation.getMenu().getItem(index).setChecked(true);
        viewPager.setCurrentItem(index);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    viewPager.setCurrentItem(0);
                    return true;
                case R.id.navigation_market:
                    viewPager.setCurrentItem(1);
                    return true;
                case R.id.navigation_find:
                    viewPager.setCurrentItem(2);
                    return true;
                case R.id.navigation_self:
                    viewPager.setCurrentItem(3);
                    return true;
                case R.id.navigation_mine:
                    viewPager.setCurrentItem(4);
                    return true;
            }
            return false;
        }

    };

}
